package com.gg.registration.cmn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gg.registration.cmn.mapper.DictMapper;
import com.gg.registration.cmn.service.DictService;
import com.gg.registration.model.cmn.Dict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjunjia
 * @date 2022/4/6 21:43
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     * 通过id值获取孩子字段
     *
     * @Cacheable :
     *    根据方法对其返回结果进行缓存，下次请求时，如果缓存存在，则直接读取缓存数据返回
     *    如果缓存不存在，则执行方法，并把返回的结果存入缓存中。一般用在查询方法上
     *
     * @param id 数据库id值
     * @return 返回带有子字段集合
     */
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public List<Dict> getChildById(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> dictList = baseMapper.selectList(wrapper);
        for (Dict dict : dictList){
            if (this.hasChildren(dict.getId())){
                dict.setHasChildren(true);
                continue;
            }
            dict.setHasChildren(false);
        }
        return dictList;
    }

    /**
     * 查询数据字典中的名字
     * @param dictCode 编码，注明了表中属性的种类
     * @param value 值
     * @return 统一返回结果
     */
    @Cacheable(value = "dictName",keyGenerator = "keyGenerator")
    @Override
    public String getName(String dictCode, String value) {
        if ("" == dictCode){
            //只根据value查询
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper);
            return dict.getName();
        }else {
            Dict one = getDictByDictCode(dictCode);
            //用此ID作为别的字段的parentId
            Long id = one.getId();
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>()
                                            .eq("parent_id", id)
                                            .eq("value", value));
            return dict.getName();
        }
    }

    /**
     * 根据dictCode查询子成员
     * @param dictCode dict类型
     * @return 统一返回结果集
     */
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public List<Dict> getChildByDictCode(String dictCode) {
        //先根据dictCode查出Dict
        Dict dict = this.getDictByDictCode(dictCode);
        //再根据Dict的id查子成员
        List<Dict> childList = this.getChildById(dict.getId());

        return childList;
    }

    /**
     * 查询节点是否有孩子
     * @param id 数据库id值
     * @return TRUE 有孩子，FALSE 无孩子
     */
    private boolean hasChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer result = baseMapper.selectCount(wrapper);
        return result>0;
    }

    /**
     * 私有方法，内部使用，根据dictCode查询Dict对象
     * @param dictCode 编码
     */
    private Dict getDictByDictCode(String dictCode){
        //根据dictCode和value查询
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);
        Dict one = baseMapper.selectOne(wrapper);
        return one;
    }
}
